package com.qiantu.phone.ui.shotview;

import android.content.Context;

import androidx.lifecycle.LifecycleOwner;

import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.DeviceCustomKeyBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.uiutil.LLDeviceUtil;
import com.qiantu.phone.widget.btnview.OnBtnClickListener;
import com.qiantu.phone.widget.btnview.OneTwoThreeFourSixEightNineButtonView;

import java.util.List;

import okhttp3.Call;

/**
 * @ClassName DeviceLightView
 * @Description TODO
 * @Author ShiJunqiang
 * @Date 2021/12/8 23:05
 * @Version 1.0
 */
public class ThreeKeyShotView extends KeyDeviceActView {

    private OneTwoThreeFourSixEightNineButtonView buttonView;
    private String key;

    public ThreeKeyShotView(Context context, String deviceType) {
        super(context, deviceType);
    }

    public void setDevice(DeviceBean device){
        mDevice = device;
    }

    @Override
    public int getLayoutId() {
        return R.layout.three_key_shot_view;
    }

    @Override
    public void initView() {
        super.initView();
        buttonView = findViewById(R.id.btn_view);
        if(deviceType.contains("wireless_433")){
            key = LLDeviceUtil.WIRELESS_CTRL_433;
        }else if(deviceType.contains("wireless_315")){
            key = LLDeviceUtil.WIRELESS_CTRL_315;
        }else if(deviceType.contains("infra-red")){
            key = LLDeviceUtil.IR_CTRL;
        }
        buttonView.setOnBtnClickListener(new OnBtnClickListener() {
            @Override
            public void onClick(int btnID) {
                if(mDevice != null){
                    addAct(key, String.valueOf(btnID), mDevice.getDeviceSerialNo());
                }else{
                    addAct(key, String.valueOf(btnID));
                }
            }
        });
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if(mDevice != null){
            updateCustomKey(mDevice.getDeviceCustomKeys());
            getDeviceCustomKeyList();
        }
    }

    private void getDeviceCustomKeyList() {
//        LLHttpManager.getDeviceCustomKeyList((LifecycleOwner) getContext(), mDevice.getDeviceSerialNo(), new HttpCallback<HttpData<DeviceCustomKeyBean>>(null) {
//            @Override
//            public void onStart(Call call) {
//
//            }
//
//            @Override
//            public void onSucceed(HttpData<DeviceCustomKeyBean> result) {
//                List<DeviceCustomKeyBean.KeyData> customKeyList = result.getData().getDeviceCustomKeys();
//                updateCustomKey(customKeyList);
//                mDevice.setDeviceCustomKeys(customKeyList);
//                LLDeviceDBManager.getInstance(getContext()).put(mDevice);
//            }
//
//            @Override
//            public void onFail(Exception e) {
//
//            }
//
//            @Override
//            public void onEnd(Call call) {
//                ((AppActivity) getContext()).hideDialog();
//            }
//        });
    }

    private void updateCustomKey(List<DeviceCustomKeyBean> customKeyList) {
        String defaultName = getResources().getString(R.string.custom_name);
        String key1 = defaultName;
        String key2 = defaultName;
        String key3 = defaultName;
        if (customKeyList != null) {
            for (DeviceCustomKeyBean keyData : customKeyList) {
                switch (keyData.getKeyIndex()) {
                    case "1":
                        key1 = keyData.getKeyName();
                        break;
                    case "2":
                        key2 = keyData.getKeyName();
                        break;
                    case "3":
                        key3 = keyData.getKeyName();
                        break;
                }
            }
        }
        buttonView.setText(key1, key2, key3);
    }

    @Override
    public void updateView() {
    }
}
